/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.component.kj;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.just.dusin.ooii.component.Sink;
import org.just.dusin.ooii.database.CLYCBean;
import org.just.dusin.ooii.database.DataOf5mBean;
import org.just.dusin.ooii.database.DayoutDao;
import org.just.dusin.ooii.database.FaultDao;
import org.just.dusin.ooii.database.HouroutDao;
import org.just.dusin.ooii.database.MineData;
import org.just.dusin.ooii.database.MonthoutDao;
import org.just.dusin.ooii.database.RealDeviceDao;
import org.just.dusin.ooii.database.RealoutDao;
import org.just.dusin.ooii.database.Sumof5minhouroutDao;
import org.just.dusin.ooii.database.YearoutDao;
import org.dusin.webcoal.exception.OOIIException;
import org.just.dusin.ooii.database.CLCSBean;
import org.just.dusin.ooii.database.DatabaseConnectionSQL;
import org.just.dusin.ooii.database.OrgDao4SQLServer;
import org.just.dusin.ooii.database.QHJCXXBDao;
import org.just.dusin.ooii.database.YearPlanDao;
import org.just.dusin.ooii.component.kj.DataPackage;
import org.just.dusin.ooii.util.DateUtil;

/**
 *
 * @author Administrator
 */
public class MDatabaseSink implements Sink {

    private static Logger logger = Logger.getLogger(MDatabaseSink.class.getName());
    RealoutDao realDao = null;
    HouroutDao hourDao = null;
    DayoutDao dayDao = null;
    MonthoutDao monthDao = null;
    YearoutDao yearDao = null;
    Sumof5minhouroutDao s5Dao = null;
    RealDeviceDao realDeviceDao = null;
    FaultDao faultDao = null;
    OrgDao4SQLServer orgDao = null;
    YearPlanDao yearPlanDao = null;
    QHJCXXBDao qhjcxxbDao = null;
    DatabaseConnectionSQL dcu = null;

    public DatabaseConnectionSQL getDcu() {
        return dcu;
    }

    public void setDcu(DatabaseConnectionSQL dcu) {
        this.dcu = dcu;
    }

    @Override
    public void process(String msg) throws OOIIException {
        //DatabaseConnectionSQL dcu = new DatabaseConnectionSQL();        
        try {
            realDao = new RealoutDao(dcu.getConnection());
            hourDao = new HouroutDao(dcu.getConnection());
            monthDao = new MonthoutDao(dcu.getConnection());
            dayDao = new DayoutDao(dcu.getConnection());
            yearDao = new YearoutDao(dcu.getConnection());
            s5Dao = new Sumof5minhouroutDao(dcu.getConnection());
            realDeviceDao = new RealDeviceDao(dcu.getConnection());
            faultDao = new FaultDao(dcu.getConnection());
            orgDao = new OrgDao4SQLServer(dcu.getConnection());
            qhjcxxbDao = new QHJCXXBDao(dcu.getConnection());
            yearPlanDao = new YearPlanDao(dcu.getConnection());
            dcu.getConnection().setAutoCommit(false);
        } catch (SQLException sqle) {
            dcu.close();
            sqle.printStackTrace();
            throw new OOIIException(sqle);
        }

        DataPackage.setOrgDao(orgDao);
        logger.info("准备处理" + msg);
        List<MineData> mineDatas = DataPackage.dePack(msg);  //重复数据要排除
        for (MineData mineData : mineDatas) {
            if (mineData instanceof DataOf5mBean) {
                DataOf5mBean d5m = (DataOf5mBean) mineData;
                /*Calendar calendar = Calendar.getInstance();   //将时间调整-5min                
                
                int year = d5m.getYear();
                int month = d5m.getMonth();
                int day = d5m.getDay();
                int hour = d5m.getHour();
                int min = d5m.getMin();
                calendar.set(d5m.getYear(), d5m.getMonth() - 1, d5m.getDay(), d5m.getHour(), d5m.getMin());
                calendar.add(Calendar.MINUTE, -5);
                year = calendar.get(Calendar.YEAR);
                month = calendar.get(Calendar.MONTH) + 1;
                day = calendar.get(Calendar.DATE);
                hour = calendar.get(Calendar.HOUR_OF_DAY);
                min = calendar.get(Calendar.MINUTE);
                d5m.setYear((short)year);
                d5m.setMonth((byte)month);
                d5m.setDay((byte)day);
                d5m.setHour((byte)hour);
                d5m.setMin((byte)min);*/
                try {
                    dcu.getConnection().setAutoCommit(false);
                    Date previous = realDao.getCurrentTime(d5m.getMineCode());
                    Date currentDataTime = DateUtil.get(d5m.getYear(), d5m.getMonth(), d5m.getDay(), d5m.getHour(), d5m.getMin(), 0);

                    if (currentDataTime.after(previous)) {
                        realDao.doUpdateOut(d5m);
                        s5Dao.doCreate(d5m);
                        hourDao.doUpdateOut(d5m);
                        dayDao.doUpdateOut(d5m);                        
                        monthDao.doUpdateOut(d5m);
                        yearDao.doUpdateOut(d5m);                                              
                    }
                    dcu.getConnection().commit();
                } catch (Exception ex) {

                    ex.printStackTrace();
                    try {
                        dcu.getConnection().rollback();
                    } catch (SQLException ex1) {

                        throw new OOIIException(ex1);
                    }
                    throw new OOIIException(ex);
                }
            } else if (mineData instanceof CLYCBean) {
                try {

                    CLYCBean clyc = (CLYCBean) mineData;
                    String orgCode = clyc.getMineCode();

                    dcu.getConnection().setAutoCommit(false);
                    Date previous = realDeviceDao.getCurrentTime(orgCode);
                    if (previous == null) {
                        previous = DateUtil.get(1900, 01, 1, 0, 0, 0);
                    }

                    Date currentDataTime = DateUtil.get(clyc.getYear(), clyc.getMonth(), clyc.getDay(), clyc.getHour(), clyc.getMin(), 59);

                    if (currentDataTime.after(previous)) {
                        String status = clyc.getStatus();
                        realDeviceDao.updateYCStatus(status, orgCode, currentDataTime);
                        faultDao.updateByStatus(status,orgCode,currentDataTime);
                    }
                    dcu.getConnection().commit();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    try {
                        dcu.getConnection().rollback();
                    } catch (SQLException ex1) {
                        throw new OOIIException(ex1);
                    }
                    throw new OOIIException(ex);
                }
            } else if (mineData instanceof CLCSBean) {
                try {

                    CLCSBean clcs = (CLCSBean) mineData;
                    String orgCode = clcs.getMineCode();

                    dcu.getConnection().setAutoCommit(false);
                    Date previous = qhjcxxbDao.getLatestTime(orgCode);
                    if (previous == null) {
                        previous = DateUtil.get(1900, 01, 1, 0, 0, 0);
                    }

                    Date currentDataTime = DateUtil.get(clcs.getYear(), clcs.getMonth(), clcs.getDay(), clcs.getHour(), clcs.getMin(), 59);

                    if (currentDataTime.after(previous)) {
                        qhjcxxbDao.doCreate(clcs);
                        yearPlanDao.doUpdate(clcs);
                    }
                    dcu.getConnection().commit();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    try {
                        dcu.getConnection().rollback();
                    } catch (SQLException ex1) {
                        throw new OOIIException(ex1);
                    }
                    throw new OOIIException(ex);
                }
            }
        }
        dcu.close();
    }

    @Override
    public void process(List<String> msgs) throws OOIIException {
        for (String msg : msgs) {
            process(msg);
        }
    }

    @Override
    public void initComponent(Properties props) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void destroyComponent() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
